<?php $this->beginBlock('title');?>
    联系我们
<?php $this->endBlock();?>
<?php $this->params['breadcrumbs'] =
    [
        [
            'label' => '联系我们',
//            'url' => \yii\helpers\Url::to(['admin/case/add']),
            'class' => 'active',
        ]
    ];
?>

<?php $this->beginBlock('headerCssFile')?>
    <link rel="stylesheet" type="text/css" href="/plugins/layui/css/layui.css">
<?php $this->endBlock()?>

    <div class="row">
        <div class="col-md-12">

            <div class="block-flat">
                <div class="header">
                    <h4>联系我们</h4>
                </div>
                <div class="content">
                    <form class="form-horizontal group-border-dashed" id="contactData">
                        <input type="hidden" name="id" value="<?php echo !empty($contact['id']) ? $contact['id'] : ''?>">
                        <input name="<?= Yii::$app->request->csrfParam;?>" type="hidden" value="<?= Yii::$app->request->csrfToken ?>">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">电话</label>
                            <div class="col-sm-9">
                                <input type="text" name="tel" value="<?php echo !empty($contact['tel']) ? $contact['tel'] : ''?>" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">QQ</label>
                            <div class="col-sm-9">
                                <input type="text" name="qq" value="<?php echo !empty($contact['qq']) ? $contact['qq'] : ''?>" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">微信</label>
                            <div class="col-sm-9">
                                <input type="text" name="wechat" value="<?php echo !empty($contact['wechat']) ? $contact['wechat'] : ''?>" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">地址</label>
                            <div class="col-sm-9">
                                <textarea name="address" class="form-control"><?php echo !empty($contact['address']) ? $contact['address'] : ''?></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="button" onclick="saveInfo.saveData()" class="btn btn-primary"><i class="fa fa-save"></i>保存</button>
                                <button class="btn btn-default"><i class="fa fa-mail-reply" onclick="history.go(-1)"></i>返回</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>

        </div>
    </div>
<?php $this->beginBlock('footerJsFile')?>
    <script src="/plugins/layui/layui.js"></script>
<?php $this->endBlock()?>

<?php $this->beginBlock('footerJsText')?>

    <script type="text/javascript">
        var layedit,layerIndex;
        // 编辑器相关操作
        layui.use('layedit', function(){
            layedit = layui.layedit
                ,$ = layui.jquery;
            // 初始化图片上传
            layedit.set({
                uploadImage: {
                    url: '<?php echo \yii\helpers\Url::to(['admin/api/upload/content'])?>' //接口url
                    ,type: 'post' //默认post
                    ,data:{
                        '<?php echo Yii::$app->request->csrfParam?>':'<?php echo Yii::$app->request->csrfToken?>',
                        category:'image',
                        action:'contact'

                    }//额外数据
                }
            });

            //构建一个默认的编辑器
            layerIndex = layedit.build('editor');
        });

        //多文件列表示例
        layui.use('upload', function() {
            var $ = layui.jquery
                , upload = layui.upload;
            var thumbList = $('#thumbList')
                , uploadListIns = upload.render({
                elem: '#upload'
                , url: '/upload/'
                , accept: 'file'
                , multiple: true
                , auto: false
                , bindAction: '#uploadAction'
                , choose: function (obj) {
                    var files = obj.pushFile(); //将每次选择的文件追加到文件队列

                    //读取本地文件
                    obj.preview(function (index, file, result) {
                        var tr = $(['<tr id="upload-' + index + '">'
                            , '<td class="text-center">' + file.name + '</td>'
                            , '<td class="text-center">' + (file.size / 1014).toFixed(1) + 'kb</td>'
                            , '<td class="text-center">等待上传</td>'
                            , '<td class="text-center">'
                            , '<button class="btn btn-primary btn-xs layui-hide demo-reload">重传</button>'
                            , '<button class="btn btn-warning btn-xs demo-delete">删除</button>'
                            , '</td>'
                            , '</tr>'].join(''));

                        //单个重传
                        tr.find('.demo-reload').on('click', function () {
                            obj.upload(index, file);
                        });

                        //删除
                        tr.find('.demo-delete').on('click', function () {
                            delete files[index]; //删除对应的文件
                            tr.remove();
                        });

                        thumbList.append(tr);
                    });
                }
                , done: function (res, index, upload) {
                    if (res.code === '0') { //上传成功
                        var tr = thumbList.find('tr#upload-' + index)
                            , tds = tr.children();
                        tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                        tds.eq(3).html(''); //清空操作
                        delete files[index]; //删除文件队列已经上传成功的文件
                        return;
                    }
                    this.error(index, upload);
                }
                , error: function (index, upload) {
                    var tr = thumbList.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                    tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                }
            });
        });

        var saveInfo = {
            saveData:function () {
                layedit.sync(layerIndex);
                var data = $("#contactData").serialize();
                ajaxLock = false;
                if (ajaxLock)return false;
                ajaxLock = true;
                $.ajax({
                    type:'POST',
                    dataType:'json',
                    data:data,
                    url:'<?php echo \yii\helpers\Url::to(['admin/contact/save'])?>',
                    success:function (resp) {
                        layer.msg(resp.m);
                    },
                    error:function () {
                        console.log("save失败");
                    }
                });
            }
        };
    </script>
<?php $this->endBlock()?>